angular.module('RDash')
    .controller('BombNoticeCtrl', ['app', '$scope', 'restService', function (app, $scope, restService) {
        console.log('bomb_notice-ctrl');
    }])
    .controller('BombCtrl', ['app', '$scope', 'restService', function (app, $scope, restService) {
        console.log('bomb-ctrl');

        $scope.bombAttrs = app.bombAttrs;
        $scope.includePath = 'templates/bomb_notice.html';
        if ($scope.bombAttrs['includePath'] != null) {
            $scope.includePath = $scope.bombAttrs['includePath'];
        }

        $scope.setIncludePath = function (includePath) {
            $scope.includePath = includePath;
            $scope.bombAttrs['includePath'] = includePath;
            app.setBombAttrs($scope.bombAttrs);
        };
    }])
    .controller('FilterChainCtrl', ['app', '$scope', 'restService', function (app, $scope, restService) {
        console.log('filter_chain-ctrl');
    }])
    .controller('FilterRunCtrl', ['app', '$scope', 'restService', function (app, $scope, restService) {
        console.log('filter_run-ctrl');

        $scope.chains = [];
        $scope.watchChains = function () {
            restService.api.bomb.save({ID: 'watchChains'}, null, function (result) {
                if (app.catchResultCode(result['resultCode'])) {
                    $scope.chains = result.data;
                }
            })
        };

        $scope.watchChains();
    }])
    .controller('FilterConfigCtrl', ['app', '$scope', 'restService', function (app, $scope, restService) {
        console.log('filter_config-ctrl');

        $scope.chains = [];
        $scope.watchConfigChains = function () {
            restService.api.bomb.save({ID: 'watchConfigChains'}, null, function (result) {
                if (app.catchResultCode(result['resultCode'])) {
                    $scope.chains = result.data;
                }
            })
        };

        $scope.watchConfigChains();
    }])
    .controller('FilterCustomCtrl', ['app', '$scope', 'restService', 'commonService', '$window', function (app, $scope, restService, commonService, $window) {
        console.log('filter_custom-ctrl');

        $scope.chains = [];
        $scope.watchCustomChains = function () {
            restService.api.bomb.save({ID: 'watchCustomChains'}, null, function (result) {
                if (app.catchResultCode(result['resultCode'])) {
                    $scope.chains = result.data;
                }
            })
        };
        $scope.createFilterChain = function () {
            commonService.openJsonEditor('创建拦截器链', {
                name: '',
                url: '',
                authorization: '',
                priority: 0
            }, function (json) {
                restService.api.bomb.save({ID: 'postFilterChain'}, json,
                    function (result) {
                        if (app.catchResultCode(result['resultCode'])) {
                            $window.location.reload();
                        }
                    });
            }, function () {
            });
        };
        $scope.reloadFilterChains = function () {
            var content = '重新启动[拦截器链]？\n';
            commonService.openMessageDialog('警告:', content, function () {
                restService.api.bomb.save({
                    ID: 'reloadFilterChains',
                    loadConfigChains: false
                }, null, function (result) {
                    if (app.catchResultCode(result['resultCode'])) {
                        // 执行成功则刷新此页面
                        $window.location.reload();
                    }
                });
            }, function () {
            });
        };
        $scope.resetFilterChains = function () {
            var content = '初始化[拦截器链]？\n';
            commonService.openMessageDialog('警告:', content, function () {
                restService.api.bomb.save({ID: 'resetFilterChains'}, null,
                    function (result) {
                        if (app.catchResultCode(result['resultCode'])) {
                            // 执行成功则刷新此页面
                            $window.location.reload();
                        }
                    });
            }, function () {
            });
        };
        $scope.alterFilterChain = function (chain) {
            chain = angular.fromJson(angular.toJson(chain));
            commonService.openJsonEditor('修改拦截器链', chain, function (json) {
                    restService.api.bomb.save({ID: 'alterFilterChain'}, json,
                        function (result) {
                            if (app.catchResultCode(result['resultCode'])) {
                                commonService.toast.putOToast({
                                    method: 'success',
                                    info: '拦截器链修改成功！'
                                });
                                $window.location.reload();
                            }
                        });
                }, function () {
                }
            );
        };
        $scope.removeFilterChain = function (chain) {
            var content = '您确定要删除 <' + chain.name + '> [' + chain.url + '] 吗？\n';
            content += '此操作所涉及的数据不可恢复,请谨慎操作！\n';
            commonService.openMessageDialog('警告:', content, function () {
                restService.api.bomb.save({
                    ID: 'removeFilterChain',
                    id: chain.id
                }, null, function (result) {
                    if (app.catchResultCode(result['resultCode'])) {
                        // 执行成功则刷新此页面
                        $window.location.reload();
                    }
                });
            }, function () {
            });
        };

        $scope.watchCustomChains();
    }]);
